package com.example.dbcms.controller.admin;

import com.example.dbcms.annotation.Log;
import com.example.dbcms.domain.Parking;
import com.example.dbcms.service.ParkingService;
import com.example.dbcms.utils.ResultVoUtil;
import com.example.dbcms.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin/parkingSpace")
@Api("停车位信息操作")
public class ParkingController {

    @Autowired
    private ParkingService parkingService;

    /**
     * 查询所有停车位信息
     *
     * @return
     */
    @GetMapping("/parkingSpaceInfo")
    @ApiOperation("查询所有停车位信息")
    @Log(type="查询所有停车位信息")
    public String parkingSpaceInfo(Parking parking,ModelMap modelMap) {
        PageInfo<Parking> parkingPageInfo = parkingService.selectAllParking(parking);
        modelMap.put("parkingInfoList", parkingPageInfo.getList());
        modelMap.put("parkingInfo", parking);
        modelMap.put("pageInfo", parkingPageInfo);
        return "admin/parking/index";
    }

    /**
     * 跳转到停车位编辑页面
     * @param id
     * @return
     */
    @ApiOperation("跳转到编辑停车位页面")
    @GetMapping("/editParking/{id}")
    @Log(type="跳转到编辑停车位页面")
    public String editParking(@PathVariable("id") int id,ModelMap modelMap){
        modelMap.put("parkingInfo", parkingService.getParkingInfo(id));
        return "admin/parking/edit";
    }

    /**
     * 修改停车位信息到持久层
     * @param parking
     * @return
     */
    @ApiOperation("修改停车位信息到持久层")
    @PostMapping("/editParking")
    @ResponseBody
    @Log(type="修改停车位信息到持久层")
    public ResultVo saveParking(Parking parking){
        int result = parkingService.updateParkingInfo(parking);
        if (result==1){
            return ResultVoUtil.success("修改成功");
        }
        return ResultVoUtil.error("修改失败");
    }

    /**
     * 跳转到详细停车位页面
     * @param id
     * @param modelMap
     * @return
     */
    @GetMapping("/detailParking/{id}")
    @ApiOperation("跳转到详细停车位页面")
    @Log(type="跳转到详细停车位页面")
    public String detailParkingInfo(@PathVariable("id") int id,ModelMap modelMap){
        modelMap.put("parkingInfo", parkingService.getParkingInfo(id));
        return "admin/parking/detail";
    }

    /**
     * 根据id删除停车位信息
     * @param id
     * @return
     */
    @ApiOperation("删除停车位信息")
    @GetMapping("/deleteParking/{id}")
    @ResponseBody
    @Log(type="删除停车位信息")
    public ResultVo deleteParking(@PathVariable("id") int id){
        int result = parkingService.deleteParkingInfo(id);
        if(result==1){
            return ResultVoUtil.success("删除成功");
        }
        return ResultVoUtil.error("删除失败");
    }

    /**
     * 删除多条记录
     * @param ids 停车位信息id列表
     * @return
     */
    @ApiOperation("删除多条居民信息")
    @PostMapping("/deleteMultiple")
    @ResponseBody
    @Log(type="删除多条居民信息")
    public ResultVo deleteMultipleParking(@RequestParam(value = "ids") List<Integer> ids){
        int result = parkingService.deleteMultipleParkingInfo(ids);
        if (result== ids.size()){
            return ResultVoUtil.success("删除成功");
        }
        return ResultVoUtil.error("删除失败");
    }

    /**
     * 添加停车位信息
     * @return
     */
    @ApiOperation("添加停车位信息")
    @GetMapping("/add")
    @Log(type="添加停车位信息")
    public String addParking(){
        return "admin/parking/add";
    }

    /**
     * 新增停车位信息到数据库
     * @param parking
     * @return
     */
    @PostMapping("/addParking")
    @ResponseBody
    @ApiOperation("新增停车位信息到数据库")
    @Log(type="新增停车位信息到数据库")
    public ResultVo addParkingToDb(Parking parking){
        int result = parkingService.addParking(parking);
        if(result == 1){
            return ResultVoUtil.SAVE_SUCCESS;
        }
        return ResultVoUtil.error("添加失败");
    }
}
